package com.nest4cloud.nest.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nest4cloud.nest.account.entity.AccountTypeEntity;
import com.nest4cloud.nest.account.mapper.AccountTypeMapper;
import com.nest4cloud.nest.account.service.AccountTypeService;
import com.nest4cloud.nest.common.security.util.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账户类型表
 *
 * @author nest
 * @date 2024-11-25 09:29:03
 */
@Service
public class AccountTypeServiceImpl extends ServiceImpl<AccountTypeMapper, AccountTypeEntity> implements AccountTypeService {

	@Override
	public boolean saveEntity(AccountTypeEntity accountType) {
		// 添加用户ID
		accountType.setUserId(SecurityUtils.getUser().getId());
		return this.save(accountType);
	}

	/**
	 * 获取用户下的账户类型
	 *
	 * @return
	 */
	@Override
	public Map<Integer, Object> getAccountTypeByUserId() {

		// 资金账户
		List<AccountTypeEntity> fundProperty = this.baseMapper.selectList(
				new LambdaQueryWrapper<AccountTypeEntity>()
//						.eq(AccountTypeEntity::getUserId, SecurityUtils.getUser().getId())
						.eq(AccountTypeEntity::getProperty, 1)
		);

		// 信用卡账户
		List<AccountTypeEntity> creditCardProperty = this.baseMapper.selectList(
				new LambdaQueryWrapper<AccountTypeEntity>()
						.eq(AccountTypeEntity::getUserId, SecurityUtils.getUser().getId())
						.eq(AccountTypeEntity::getProperty, 2)
		);

		// 充值账户
		List<AccountTypeEntity> rechargeProperty = this.baseMapper.selectList(
				new LambdaQueryWrapper<AccountTypeEntity>()
						.eq(AccountTypeEntity::getUserId, SecurityUtils.getUser().getId())
						.eq(AccountTypeEntity::getProperty, 3)
		);

		// 投资理财账户
		List<AccountTypeEntity> investmentProperty = this.baseMapper.selectList(
				new LambdaQueryWrapper<AccountTypeEntity>()
						.eq(AccountTypeEntity::getUserId, SecurityUtils.getUser().getId())
						.eq(AccountTypeEntity::getProperty, 4)
				);

		// 债务
		List<AccountTypeEntity> debtProperty = this.baseMapper.selectList(
				new LambdaQueryWrapper<AccountTypeEntity>()
						.eq(AccountTypeEntity::getUserId, SecurityUtils.getUser().getId())
						.eq(AccountTypeEntity::getProperty, 5)
				);

		// 其他
		List<AccountTypeEntity> otherProperty = this.baseMapper.selectList(
				new LambdaQueryWrapper<AccountTypeEntity>()
						.eq(AccountTypeEntity::getUserId, SecurityUtils.getUser().getId())
						.eq(AccountTypeEntity::getProperty, 6)
				);

		Map<Integer, Object> result = new HashMap<>();
		result.put(1, fundProperty);
		result.put(2, creditCardProperty);
		result.put(3, rechargeProperty);
		result.put(4, investmentProperty);
		result.put(5, debtProperty);
		result.put(6, otherProperty);

		return result;
	}

	/**
	 * 根据账户类型ID获取账户类型
	 *
	 * @param accountTypeId 账户类型ID
	 * @return 账户类型
	 */
	@Override
	public AccountTypeEntity getAccountTypeById(Long accountTypeId) {
		return this.baseMapper.selectById(accountTypeId);
	}
}
